package com.kly.cashmall.module.products.presenter;

import com.google.gson.JsonArray;
import com.kly.cashmall.bean.IndexProductListBean;
import com.kly.cashmall.framework.mvp.BaseViewPresenter;
import com.kly.cashmall.services.Network;
import com.kly.cashmall.services.Params;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * @author
 */
public class ProductsPresenter extends BaseViewPresenter<ProductsViewer> {
    public ProductsPresenter(ProductsViewer viewer) {
        super(viewer);
    }

    @Override
    public void willDestroy() {

    }

    public void getProductList(int page, JsonArray filterConditions) {
        Params params = new Params();
        params.put("filterConditions", filterConditions);
        params.put("currentPage", page);
        rxManager.add(Network.getApi().getProductList(params.buildPostBody()).subscribeOn(Schedulers.io()).observeOn
                (AndroidSchedulers.mainThread()).subscribe(new Consumer<IndexProductListBean>() {
            @Override
            public void accept(IndexProductListBean bean) throws Exception {
                if (getViewer() != null) {
                    if (page == 1) {
                        getViewer().onProductListRefreshingSuccess(bean);
                    } else {
                        getViewer().onProductListLoadmoreSuccess(bean);
                    }
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                if (getViewer() != null) {
                    if (page == 1) {
                        getViewer().onProductListRefreshingFailed();
                    } else {
                        getViewer().onProductListLoadmoreFailed();
                    }
                }
            }
        }));
    }
}
